package com.hxzy.service.impl;

import com.hxzy.util.MybatisUtil;
import com.hxzy.dto.UserInfoDTO;
import com.hxzy.entity.UserInfo;
import com.hxzy.mapper.UserInfoMapper;
import com.hxzy.service.UserInfoService;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

/**
 * @Description:
 * @Author: ykj
 * @Date: 2022/2/26 17:03
 */
public class UserInfoServiceImpl implements UserInfoService {
    @Override
    public int insert(UserInfo userInfo) {
        int count=0;
        try (
                SqlSession sqlSession= MybatisUtil.getSqlSessionFactory().openSession()
                ){
            UserInfoMapper mapper =  sqlSession.getMapper(UserInfoMapper.class);
            count = mapper.insert(userInfo);
            sqlSession.commit();
        }
        return count;
    }

    @Override
    public int update(UserInfo userInfo) {
        int count=0;
        try(SqlSession sqlSession=MybatisUtil.getSqlSessionFactory().openSession()){
            UserInfoMapper mapper = sqlSession.getMapper(UserInfoMapper.class);
            count = mapper.update(userInfo);
            sqlSession.commit();
        }
        return count;
    }


    @Override
    public int delete(Integer id) {
        int count=0;
        try(SqlSession sqlSession=MybatisUtil.getSqlSessionFactory().openSession()){
            UserInfoMapper mapper = sqlSession.getMapper(UserInfoMapper.class);
            count=mapper.delete(id);
            sqlSession.commit();
        }
        return count;
    }

    @Override
    public UserInfo findOne(Integer id) {
        UserInfo userInfo=null;
        try(SqlSession sqlSession=MybatisUtil.getSqlSessionFactory().openSession()){
            UserInfoMapper mapper = sqlSession.getMapper(UserInfoMapper.class);
             userInfo = mapper.findOne(id);
             sqlSession.commit();
        }
        return userInfo;
    }

    @Override
    public UserInfo findLoginName(UserInfoDTO loginName) {
        UserInfo name=null;
        try(SqlSession sqlSession=MybatisUtil.getSqlSessionFactory().openSession())
        {
            UserInfoMapper mapper = sqlSession.getMapper(UserInfoMapper.class);
             name = mapper.findLoginName(loginName);
            sqlSession.commit();
        }
        return name;
    }

    @Override
    public List<UserInfo> findAll(UserInfoDTO userInfoDTO) {
        List<UserInfo> arr=null;
        try(SqlSession sqlSession=MybatisUtil.getSqlSessionFactory().openSession()){
            UserInfoMapper mapper = sqlSession.getMapper(UserInfoMapper.class);
            arr = mapper.findAll(userInfoDTO);
        }
        System.out.println(arr);
        return arr;
    }

    @Override
    public UserInfo searchByName(String loginName) {
        UserInfo userInfo=null;
        try (SqlSession sqlSession=MybatisUtil.getSqlSessionFactory().openSession()){
            UserInfoMapper mapper = sqlSession.getMapper(UserInfoMapper.class);
            userInfo = mapper.searchByName(loginName);
        }
        return userInfo;
    }

    @Override
    public int existsLoginName(UserInfo userInfo) {
        int count=0;
        try(SqlSession sqlSession=MybatisUtil.getSqlSessionFactory().openSession()){
            UserInfoMapper mapper = sqlSession.getMapper(UserInfoMapper.class);
            count = mapper.existsLoginName(userInfo);

        }
        return  count;
    }
}
